package com.eolwral.osmonitor;

import java.util.StringTokenizer;

import android.util.Log;


public class MemStat {
	private static long Total;
	private static long Free;
	private static long Cached;
	private static long Buffers;
	
	public static void Update() {
		try {
			String line = CommonUtil.ReadFile("/proc/meminfo");
			StringTokenizer tok = new StringTokenizer(line);
			
			while (tok.hasMoreTokens())
			{
				//Get the entry
				String heading = tok.nextToken();
				if (!tok.hasMoreTokens()) {
					throw new NumberFormatException();
				}
				
				//Get the value
				long value = 0;
				{
					String strValue = tok.nextToken();
					if (!tok.hasMoreTokens()) {
						throw new NumberFormatException();
					}
					value = Integer.parseInt(strValue);
					String unit = tok.nextToken();
					if (unit.toLowerCase().equals("kb")) {
						value *= 1024;
					} 
				}
				
				if (heading.equals("MemTotal:")) {
					Total = value;
				} else if (heading.equals("MemFree:")) {
					Free = value;
				} else if (heading.equals("Buffers:")) {
					Buffers = value;
				} else if (heading.equals("Cached:")) {
					Cached = value;
				}
			}
		} catch (NumberFormatException e) {
			Log.e("osmonitor", "error parsing /proc/meminfo", e);
			return;
		}
	}
	
	public static long GetTotal() {
		return Total;
	}
	
	public static long GetFree() {
		return Free;
	}
	
	public static long GetCached() {
		return Cached;
	}
	
	public static long GetBuffers() {
		return Buffers;
	}
}
